package com.rzico.report.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.report.entity.*;
import com.rzico.report.service.OrderReportFormService;
import com.rzico.report.service.SaleRoomReportService;
import com.rzico.util.StringUtils;
import io.swagger.annotations.*;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 销售数据统计
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-11-15
 */
@Api(description = "销售数据统计")
@RestController
@RequestMapping("/report/order")
public class OrderReportFormController extends BaseController {

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private OrderReportFormService orderReportFormService;

    @ApiOperation("订单明细")
    @GetMapping("/detail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "enterpriseId", value = "商户ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "productCategoryId", value = "{0:已完成,1:未确认订单,2:已确认订单,3:待退款和已退款}", dataType = "Integer", paramType = "query")
    })
    public CommResult<SaleRoomVo> orderDetail(Date startDate,Date endDate,Integer enterpriseId,Integer type, Pageable pageable) throws ParseException {
        Date startDateTemp = DateUtils.truncate(startDate, Calendar.DATE);
        Date endDateTemp = DateUtils.addMilliseconds(DateUtils.addDays(endDate, 1), -1);
        Map<String,Object> params = new HashMap();
        if (enterpriseId != null) {
            params.put("enterpriseId", enterpriseId);
        }
        if (startDate!=null){
            params.put("startDate", startDateTemp);
        }
        if (endDate!=null){
            params.put("endDate", endDateTemp);
        }
//     -- 订单状态(0.未确认,1.已确认,2.已完成,3.已取消,4.已评价)
//     -- 支付状态(0.未支付,1.已支付,2.待退款,3.已退款)  4200000980202103260246682281   4200000980202103196578282285
//     -- 配送状态(0.未发货,1.已发货,2.待退货,3.已退货)
//     -- 订单类型(0.实时单,1.预约单)
        List<Integer> orderStatus = new LinkedList<>();
        List<Integer> payStatus = new LinkedList<>();
        if (type == null) {
            orderStatus.add(2);
            orderStatus.add(4);
            payStatus.add(1);
        } else if (type == 0) {
            orderStatus.add(2);
            orderStatus.add(4);
            payStatus.add(1);
        } else if (type == 1) {
            //查询未确认
            orderStatus.add(0);
            payStatus.add(0);
            payStatus.add(1);
            payStatus.add(2);
            payStatus.add(3);
        } else if (type == 2) {
            //查询已确认的订单
            orderStatus.add(1);
            payStatus.add(1);
            payStatus.add(2);
            payStatus.add(3);
        } else if (type == 3) {
            //待退款和已退款
            orderStatus.add(1);
            orderStatus.add(2);
            orderStatus.add(4);
            payStatus.add(2);
            payStatus.add(3);
        } else {
            orderStatus.add(2);
            orderStatus.add(4);
            payStatus.add(1);
        }
        params.put("orderStatus",orderStatus);
        params.put("paymentStatus",payStatus);
        params.put("querytype",type);

        if(StringUtils.isNotBlank(pageable.getSortField())) {
            params.put("sortField", pageable.getSortField());
            params.put("sortType", pageable.getDirection()==null?"DESC":pageable.getDirection().name());
        }


        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<OrderReportFormVo> list = orderReportFormService.query(params);
        if ("146".equals(enterpriseId) && list != null && list.size() > 0 && "阔口社区邻里中心".equals(list.get(0).getName())) {
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//            Date parse1 = sdf.parse("2021-03-03");
//            Date parse2 = sdf.parse("2021-03-04");
//
//            Date parse3 = sdf.parse(sdf.format(startDateTemp));
//            Date parse4 = sdf.parse(sdf.format(endDateTemp));
//            if (parse3.compareTo(parse1) >= 0 && parse4.compareTo(parse2) <= 0) {
                for (OrderReportFormVo ov: list) {
                    ov.setPtAmount(ov.getDiscount());
                    ov.setDiscount(new BigDecimal(0));
                }
            //}
        }

        PageResult<OrderReportFormVo> pageResult = new PageResult<OrderReportFormVo>(list, startPage.getTotal(), pageable);

        return CommResult.success(pageResult);
    }

    @ApiModelProperty(value = "每家商家日销售")
    @GetMapping("/shopSaleDetail")
    public CommResult<SaleRoomVo> shopSaleDetail(Date startDate,Date endDate,Long enterpriseId, Pageable pageable) {
        Map<String,Object> params = new HashMap();
        if (enterpriseId!=null && enterpriseId != 0) {
            params.put("enterpriseId", enterpriseId);
        }
        if (startDate!=null){
            params.put("startDate", DateUtils.truncate(startDate, Calendar.DATE));
        }
        if (endDate!=null){
            params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }
        if(StringUtils.isNotBlank(pageable.getSortField())) {
            params.put("sortField", pageable.getSortField());
            params.put("sortType", pageable.getDirection()==null?"DESC":pageable.getDirection().name());
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<ShopSaleDetailVo> list = orderReportFormService.queryShopSaleDetail(params);
        PageResult<ShopSaleDetailVo> pageResult = new PageResult<ShopSaleDetailVo>(list, startPage.getTotal(), pageable);

        return CommResult.success(pageResult);
    }

}
